﻿using System;
using System.Windows.Input;
using NLog;

namespace BHS.TFSChangsetAnalyser.ViewModel
{
    public class SaveDefaultsCommand : ICommand
    {
        private readonly IMainWindowViewModel _viewModel;
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        public SaveDefaultsCommand(IMainWindowViewModel viewModel)
        {
            _viewModel = viewModel;
        }

        public void Execute(object parameter)
        {
            Logger.Info("Start Execute");

            _viewModel.DefaultValuesProvider.UseDefaultCredentials = _viewModel.TfsServerConnectionDetails.UseDefaultCredentials;
            _viewModel.DefaultValuesProvider.Domain = _viewModel.TfsServerConnectionDetails.DomainName;
            _viewModel.DefaultValuesProvider.Username = _viewModel.TfsServerConnectionDetails.Username;
            _viewModel.DefaultValuesProvider.TfsServerPath = _viewModel.TfsServerConnectionDetails.TfsServerUrl;
            _viewModel.DefaultValuesProvider.TfsProjectPath = _viewModel.TfsServerConnectionDetails.TfsProjectPath;

            _viewModel.DefaultValuesProvider.Save();

            Logger.Info("End Execute");
        }

        public bool CanExecute(object parameter)
        {
            // Can always be executed.
            return true;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    }
}
